import json

import requests
from flask import Blueprint, render_template, request, current_app
from applications.common.utils.rights import authorize

emerg_report = Blueprint('emergencyReport', __name__, url_prefix='/emergency_report')


# 获取应急演练训练报告页面
@emerg_report.get('/report/<string:type>')
@authorize("emerg:res_report", log=True)
def page(type):
    print(type)
    if type == 'escape':
        return render_template('admin/emerg_report_escape.html')
    elif type == 'fire':
        return render_template('admin/emerg_report_fire.html')
    elif type == 'fight':
        return render_template('admin/emerg_report_fight.html')


#   应急演练训练报告分页查询
@emerg_report.get('/data/<string:type>')
@authorize("emerg:res_report", log=True)
def data(type):
    page = request.args['page']
    page_size = request.args['limit']
    response = requests.post(url=current_app.config['EMERGENCY_URL']+'/plans/records',
                             headers={
                                 'Content-type': 'application/json'
                             },
                             data=json.dumps({
                                 "page": int(page) - 1,
                                 "page_size": page_size,
                                 "total_count": True,
                                 "type": type
                             }))
    data = json.loads(response.text)
    data['code'] = 0
    return data

# 展示训练详细信息页面
@emerg_report.get('/details/<string:type>')
def details(type):
    report_detail = request.args.to_dict()
    report_detail['work_score'] = float(report_detail['work_score'])
    report_detail['process_score'] = float(report_detail['process_score'])
    report_detail['staff_score'] = float(report_detail['staff_score'])
    if type == 'fire':
        report_detail['decisionPortaton'] = float(report_detail['decisionPortaton']) * 100
        return render_template('admin/emergy_report_detail_fire.html', report_detail=report_detail)
    elif type == 'escape':
        return render_template('admin/emergency_report/escape_report.html', report_detail=report_detail)
    elif type == 'fight':
        return render_template('admin/emergy_report_detail_fight.html', report_detail=report_detail)

